package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.School;
import com.igoal.utils.StringUtils;

@Repository("schoolDAO")
public class SchoolDAOImpl extends GenericDaoHibernate<School, Long> implements SchoolDAO {

	public SchoolDAOImpl() {
		super(School.class);
	}

	public School getSchoolByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(School.class);
		dc.add(Restrictions.ilike("name", name, MatchMode.EXACT));
		getByStatus(dc, DataStatus.USING);
		List<School> schools = findByCriteria(dc);
		if (schools.size() > 0) {
			return schools.get(0);
		}

		return null;
	}
	
	public List<School> getSchoolByNameAndCity(String name, String cityName, Long cityId){
		DetachedCriteria dc = DetachedCriteria.forClass(School.class);
		if(!StringUtils.isBlank(name)){
			dc.add(Restrictions.eq("name", name));
		}
		if(!StringUtils.isBlank(cityName)){
			dc.add(Restrictions.eq("city.cityName", cityName));
		}
		if(cityId != null && cityId > 0L){
			dc.add(Restrictions.eq("city.id", cityId));
		}
		
		return findByCriteria(dc);
	}
}
